package com.yao.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.common.resp.DTree;
import com.yao.sys.entity.dto.MenuDto;
import com.yao.sys.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 菜单表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping(value = "/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Void> add(@RequestBody MenuDto model) {

        ValidateHelper.validateNull(model, new String[] {"name"});

        // 表示为一级菜单
        if (null == model.getParentId()) {
            model.setParentId(0);
        }

        menuService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Void> update(@RequestBody MenuDto model) {

        ValidateHelper.validateNull(model, new String[] {"id", "name"});

        menuService.modifyById(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "删除菜单", notes = "逻辑删除菜单")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Void> deleteById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        menuService.removeById(id);

        return ApiResponse.success();
    }

    @ApiOperation(value = "获取菜单", notes = "获取菜单")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<MenuDto> get(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(menuService.getById(id));
    }

    @ApiOperation(value = "菜单列表(分页)", notes = "菜单列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<MenuDto>> list(@RequestBody PageQueryBean<MenuDto> queryBean) {

        if (Objects.isNull(queryBean.getData())) {
            queryBean.setData(new MenuDto());
        }

        return ApiResponse
            .success(menuService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize()), queryBean.getData()));
    }

    @ApiOperation(value = "查询root菜单", notes = "查询root菜单")
    @RequestMapping(value = "/listRoot", method = RequestMethod.GET)
    public ApiResponse<List<MenuDto>> listRoot() {

        return ApiResponse.success(menuService.listRoot());
    }

    @ApiOperation(value = "查询所有菜单", notes = "查询所有菜单")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public ApiResponse<List<MenuDto>> listAll() {
        return ApiResponse.success(menuService.list());
    }

    @ApiOperation(value = "菜单树", notes = "菜单树")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public ApiResponse<DTree> tree(){

        return ApiResponse.success(menuService.tree());
    }


}